<template>
  <div class="main">
    <div class="title">
      <a class="active" href="/login">登录</a>
      <span>·</span>
      <a href="/register">注册</a>
    </div>
    <div class="sign-up-container">
      <el-form ref="userForm" :model="user">
        <el-form-item
          class="input-prepend restyle"
          prop="mobile"
          :rules="[{ required: true, message: '请输入手机号码', trigger: 'blur' },{validator: checkPhone, trigger: 'blur'}]"
        >
          <div>
            <el-input type="text" placeholder="手机号" v-model="user.mobile" />
            <i class="iconfont icon-phone" />
          </div>
        </el-form-item>
        <el-form-item
          class="input-prepend"
          prop="password"
          :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]"
        >
          <div>
            <el-input type="password" placeholder="密码" v-model="user.password" />
            <i class="iconfont icon-password" />
          </div>
        </el-form-item>
        <div class="btn">
          <input type="button" class="sign-in-button" value="登录" @click="submitLogin()" />
        </div>
        <!-- TODO 人脸登录 -->
      </el-form>
      <input type="button" class="sign-in-button" value="人脸登录" @click="faceLogin()" />
      <div class="more-sign">
        <!-- 开通人脸登录 -->
        <span>
          <a @click="dialogVisible = true">开通人脸登录</a>
        </span>
      </div>
      <!-- 更多登录方式 -->
      <div class="more-sign">
        <h6>社交帐号登录</h6>
        <ul>
          <li>
            <a
              id="weixin"
              class="weixin"
              target="_blank"
              href="http://localhost:8150/api/ucenter/wx/login"
            >
              <i class="iconfont icon-weixin" />
            </a>
          </li>
          <li>
            <a id="qq" class="qq" target="_blank" href="#">
              <i class="iconfont icon-qq" />
            </a>
          </li>
        </ul>
      </div>
    </div>
    <!-- 开通人脸登录功能 -->
    <el-dialog
      title="开通人脸登录功能"
      :visible.sync="dialogVisible"
      width="30%"
      :close-on-click-modal="false"
      :before-close="handleClose"
    >
      <el-input v-model="user.mobile" placeholder="账号手机号码"></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="next()">下一步</el-button>
      </span>
    </el-dialog>
    <!-- 展示人脸登录二维码 -->
    <el-dialog
      title="扫描二维码进行人脸检测"
      :visible.sync="showQRCode"
      width="30%"
      :close-on-click-modal="false"
      :before-close="handleClose"
    >
      <img :src="qrcode.file" alt />
    </el-dialog>
  </div>
</template>

<script>
import "~/assets/css/sign.css";
import "~/assets/css/iconfont.css";
import cookie from "js-cookie";
import loginApi from "@/api/login";
import request from "@/utils/request";
import { Message } from "element-ui";
export default {
  layout: "sign",
  data() {
    return {
      // 登录表单
      user: {
        id: "",
        mobile: "",
        password: "",
      },
      qrcode: {
        code: "",
        file: "",
      },
      // 定时器
      checkQRState: {},
      // 用户信息，显示使用
      LoginInfo: {},
      dialogVisible: false,
      showQRCode: false,
    };
  },
  created() {},
  methods: {
    submitLogin() {
      loginApi.login(this.user).then((response) => {
        // 1.获取token字符串
        // 2.把获取到的token放到cookie中
        cookie.set("guli_token", response.data.data.token, {
          domain: "localhost",
        });
        // 3.发送请求获取用户信息
        loginApi.getLoginUserInfo().then((response) => {
          // 5.获取返回的用户信息，放到cookie中
          cookie.set("guli_ucenter", response.data.data.userInfo, {
            domain: "localhost",
          });
          //alert(cookie.get("guli_ucenter"));
          // 跳转到首页
          window.location.href = "/";
        });
      });
    },
    handleClose(done) {
      clearInterval(this.checkQRState);
      done();
    },
    checkPhone(rule, value, callback) {
      //debugger
      if (!/^1[34578]\d{9}$/.test(value)) {
        return callback(new Error("手机号码格式不正确"));
      }
      return callback();
    },
    faceLogin() {
      this.showQRCode = true;
      this.getQRCode();
    },
    getQRCode() {
      loginApi.qrcode().then((res) => {
        this.qrcode = res.data.data.qrcode;
        this.checkQRState = setInterval(() => {
          loginApi.CheckQRCode(this.qrcode.code).then((res) => {
            this.faceLoginInfo = res.data.data.faceLoginInfo
            let state = this.faceLoginInfo.state;
            if (state === 1) {
              this.showQRCode = false;
              clearInterval(this.checkQRState);
              // 1.获取token字符串
              // 2.把获取到的token放到cookie中
              let token = this.faceLoginInfo.token;
              cookie.set("guli_token", token, { domain: "localhost" });
              loginApi.getLoginUserInfo().then((res) => {
                cookie.set("guli_ucenter", res.data.data.userInfo, {
                  domain: "localhost",
                });
                // 跳转到首页
                window.location.href = "/";
              });
            } else if (state === 0) {
              clearInterval(this.checkQRState);
              this.showQRCode = false;
              Message({
                message: "人脸验证失败",
                type: "error",
                duration: 5 * 1000,
              });
            }
          });
        }, 3 * 1000);
      });
    },
    next() {
      loginApi.getMemberByMobile(this.user.mobile).then((res) => {
        this.user.id = res.data.data.id;
        this.$router.push({ path: "facelogin/camera/" + this.user.id });
      });
    },
  },
};
</script>